package com.snxun.browser_light.adapter.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lodz.android.imageloaderkt.ImageLoader;
import com.lodz.android.pandora.widget.rv.recycler.BaseRecyclerViewAdapter;
import com.snxun.browser_light.R;
import com.snxun.browser_light.bean.AppInfoBean;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 界面显示app的RecyclerView的适配器
 * Created by Yangjw on 2020/11/19.
 */
public class AppRecyclerViewAdapter extends BaseRecyclerViewAdapter<AppInfoBean> {

    /**
     * App按钮回调
     */
    private AppListener mListener;

    /**
     * 构造
     *
     * @param context 上下文
     */
    public AppRecyclerViewAdapter(@NotNull Context context) {
        super(context);
    }

    @Override
    public void onBind(@NotNull RecyclerView.ViewHolder viewHolder, int i) {
        AppInfoBean infoBean = getItem(i);
        if (infoBean == null) return;
        AppViewHodler hodler = (AppViewHodler) viewHolder;
        hodler.appNametv.setText(infoBean.appName);
        setAppImage(hodler.appIconImage, infoBean.appIconUrl);
        hodler.appBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onClickAppBtnCallback(i);
            }
        });

    }


    /**
     * 设置app图标
     *
     * @param imageView 需要设置图标的控件
     * @param url       图标的路径
     */
    private void setAppImage(ImageView imageView, String url) {
        ImageLoader.create(getContext())
                .loadUrl(url)
                .setFitCenter()
                .into(imageView);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AppViewHodler(getLayoutView(parent, R.layout.item_app_rv, false));
    }

    static class AppViewHodler extends RecyclerView.ViewHolder {

        /**
         * 应用按钮
         */
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_app_btn)
        ViewGroup appBtn;
        /**
         * 应用名字
         */
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_app_name)
        TextView appNametv;
        /**
         * 应用图标
         */
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_app_icon)
        ImageView appIconImage;

        public AppViewHodler(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setAppBtnClickListener(AppListener listener) {
        this.mListener = listener;
    }

    public interface AppListener {
        void onClickAppBtnCallback(int position);
    }
}
